/**
 * use list
 */
layui.use(['form', 'table', 'tableSelect'], function () {
    var $ = layui.jquery,
        form = layui.form,
        tableSelect = layui.tableSelect,
        table = layui.table;
    let searchFieldsetStatus=localStorage.getItem("searchFieldsetStatus");
    if(searchFieldsetStatus=='0')
    {
        $(".search-display-btn").attr("show","0");
        $(".search-display-btn").html('<i class="fa fa-angle-down"></i>展开');
        $($('.layui-form')[0]).hide();
    }
    setTableHeight();
    let u=localStorage.getItem("ADMIN_LOGIN");
    let loginU={};
    if(u==undefined){
        layer.msg('请登录', {icon: 1, time: 500},function () {window.location = 'logout';});
    }else{
        loginU=decryptLocalStorage(u);
        if(loginU.userType!='1') {
            // $("#shipName").attr("disabled","disabled");
        }else{

        }
    }
    let tableUrl=baseUrl+"admin/v1/androidDevice/page?token="+loginU.token;
    let shipIds="";
    let shipNames="";
    let keyWordShipId=localStorage.getItem("keyWordShipId");
    if(keyWordShipId!=undefined){shipIds=keyWordShipId;$("#shipIds").val(shipIds);}
    let keyWordShipName=localStorage.getItem("keyWordShipName");
    if(keyWordShipName!=undefined){shipNames=keyWordShipName;$("#shipNames").val(shipNames);}
    table.render({
        elem: '#currentTableId',
        url: tableUrl,loginUserType:loginU.userType,
        where: {
            keyWork: JSON.stringify({shipIds:shipIds,shipNames:shipNames})
        },
        toolbar: '#toolbarPanel',
        defaultToolbar: ['filter', 'exports'],
        cols: [[
            {type: "checkbox", align:"center", width: 50},
            {field: 'androidDeviceId', align:"center", width: 80, title: 'ID', sort: true},
            {field: 'androidUuid', align:"center", title: '设备id', sort: true},
            {field: 'androidType', align:"center", title: '设备类型', sort: true,
                templet: function (data) {
                    if (data.systemType =='app') {
                        return "APP";
                    }else {
                        return "TV"
                    }
                }
            },
            {field: 'userKey', align:"center",  title: 'userKey', sort: true},
            {fixed: "right",title: '操作', minWidth: 220, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100, 200, 300,500],
        limit: 20,
        page: true,
        skin: 'line',parseData: function (res) {
            for (let i in res.data) {
                res.data[i].loginUserType = loginU.userType;
            }
            return res;
        },
        done: function(res, curr, count){
            if(res.code===-1)
            {
                layer.msg('请登录', {icon: 1, time: 500},function () {window.location = 'logout';});
                return;
            }
        }
    });

    // 监听搜索操作
    let keyWork="{}";
    form.on('submit(data-search-btn)', function (data) {
        let keyWordShipId=data.field.shipIds;
        let keyWordShipName=data.field.shipNames;
        if(keyWordShipId!=undefined && keyWordShipId!='undefined'){localStorage.setItem("keyWordShipId",keyWordShipId);}
        if(keyWordShipName!=undefined && keyWordShipName!='undefined'){localStorage.setItem("keyWordShipName", keyWordShipName);}

        keyWork = JSON.stringify(data.field);
        setTableHeight();
        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                keyWork: keyWork
            }
        }, 'data');

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            localStorage.removeItem('androidDeviceData');
            var index = layer.open({
                title: '添加',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: false,
                area: ['80%', '80%'],
                content: '../page/android-device/edit',
                end : function() {
                    setTableHeight();
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: {
                            keyWork: keyWork
                        }
                    }, 'data');
                }

            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            let params={token:loginU.token,ids:''};
            let ids='';
            if(data.length>0){
                for(let i=0;i<data.length;i++){
                    ids+=data[i].androidDeviceId+',';
                }
                ids=ids.substring(0,ids.length-1);
            }
            params.ids=ids;
            layer.confirm("确认删除，请谨慎操作？", {
                btn: ["确定", "取消"]
            }, function (index) {
                layer.close(index);
                request.delList('androidDevice',params,function(result){
                    if(result.code==0){
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 500
                        },function () {
                            setTableHeight();
                            //执行搜索重载
                            table.reload('currentTableId', {
                                page: {
                                    curr: 1
                                }
                                , where: {
                                    keyWork: keyWork
                                }
                            }, 'data');
                        });
                    }else{
                        layer.msg(result.msg ||'删除失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });


            }, function () {
                layer.close(index);
            });


        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        //console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
            localStorage.setItem("androidDeviceData", encryptLocalStorage(data,loginU.token));
            let index = layer.open({
                title: '编辑',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: false,
                area: ['80%', '80%'],
                content: '../page/android-device/edit',
                end : function() {
                    setTableHeight();
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: {
                            keyWork: keyWork
                        }
                    }, 'data');
                }

            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm("确认删除，请谨慎操作？", {
                btn: ["确定", "取消"]
            }, function (index) {
                layer.close(index);
                request.del('androidDevice',{token:loginU.token,id:data.androidDeviceId},function(result){
                    if(result.code==0){
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 500
                        },function () {
                            obj.del();
                        });
                    }else{
                        layer.msg(result.msg ||'删除失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });

            }, function () {
                layer.close(index);
            });


        }
    });


});